package cn.nubia.admin.sysconfig.service;

import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.sysconfig.bean.MenuResource;
import cn.nubia.admin.sysconfig.bean.RoleResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jason on 2015/6/27.
 * 角色权限服务.
 */
@Service
public class RoleResourceService extends BaseServiceImpl<RoleResource> {


    @Autowired
    private MenuResourceService menuResourceService;


    /**
     * 通过角色找到相对应的资源.
     *
     * @param id
     * @return
     */
    public List<MenuResource> findByRoleId(Integer id) {
        Query<RoleResource> query = Query.build(RoleResource.class);
        query.addEq("roleId", id);
        List<RoleResource> list = findByQuery(query);

        List<MenuResource> menuResourceList = new ArrayList<MenuResource>();

        for (RoleResource roleResource : list) {
            MenuResource menuResource = menuResourceService.get(roleResource.getResId());
            menuResourceList.add(menuResource);
        }
        return menuResourceList;

    }
}
